//
//  HealthInfoController.m
//  healthApp
//
//  Created by william mu on 12-8-28.
//  Copyright (c) 2012年 Tencent. All rights reserved.
//

#import "HealthInfoController.h"
#import "ProtocolService.h"
#import "GlobalState.h"
#import "ProtocolTranslate.h"
#import "HealthInfoCell.h"
#import "ModifyInfoController.h"
#import "HistoryInfoControllerViewController.h"

@interface HealthInfoController (Private)
-(void) startRequest;

@end

@implementation HealthInfoController
@synthesize tableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title = @"个人健康信息";
    
    m_allRequest = [[NSMutableArray array] retain];
    
    m_healthInfo = nil;
    
    [self startRequest];
}

-(void) viewWillAppear:(BOOL)animated
{

}

-(void) viewWillDisappear:(BOOL)animated
{
    ProtocolService* protocol = [ProtocolService sharedInstance];
    [protocol shutdownAnyResponse:m_allRequest];
    [m_allRequest removeAllObjects];
}

-(void) startRequest
{
    GlobalState* state = [GlobalState sharedInstance];
    
    NSString* myuid = state.uid;
    
    ProtocolService* protocol = [ProtocolService sharedInstance];
    
    [protocol setTarget:self];
    [protocol setAction:@selector(responseHealthInfo:)];
    
    NSString* seq = [protocol requestHealthInfo:myuid];
    [m_allRequest addObject:seq];
}

-(void) responseHealthInfo:(NSString*) json
{
    m_healthInfo = translate_health_info(json);
    [m_healthInfo retain];
    
    [self.tableView reloadData];
}



- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;

    self.tableView = nil;
    
    [m_allRequest release], m_allRequest = nil;
    
    [m_healthInfo release], m_healthInfo = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(!m_healthInfo)
        return 0;
    
    NSArray* fields = [m_healthInfo allKeys];
    
    return [fields count];
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

-(NSString*) getFieldNameForIndexPath:(NSIndexPath *) indexPath
{
    NSUInteger row = [indexPath row];
    NSArray* allFields = [m_healthInfo allKeys];
    
    NSString* field = (NSString*)[allFields objectAtIndex:row];

    GlobalState* state = [GlobalState sharedInstance];
    NSString* fieldName = [state.healthFieldMap objectForKey:field];
    
    return fieldName;
}

-(NSString*) getFieldForIndexPath:(NSIndexPath*) indexPath
{
    NSUInteger row = [indexPath row];
    NSArray* allFields = [m_healthInfo allKeys];
    NSString* field = (NSString*)[allFields objectAtIndex:row];
    return field;
}

-(NSString*) getFieldValueForIndexPath:(NSIndexPath*) indexPath
{
    NSString* field = [self getFieldForIndexPath:indexPath];
    

    NSString* value = (NSString*)[m_healthInfo objectForKey:field];
    
    return value;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static const NSString* identifier = @"HealthInfoCell";
    
    
    HealthInfoCell* cell = (HealthInfoCell*) [self.tableView dequeueReusableCellWithIdentifier:identifier];
    
    if(!cell){
        NSArray* objects = [[NSBundle mainBundle] loadNibNamed:@"HealthInfoCell" owner:self options:nil];
        
        cell = (HealthInfoCell*)[objects objectAtIndex:0];
    }
    

    cell.fieldLbl.text = [self getFieldNameForIndexPath:indexPath];
    cell.valueLbl.text = [self getFieldValueForIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    
    NSString* fieldName = [self getFieldNameForIndexPath:indexPath];
    NSString* fieldValue = [self getFieldValueForIndexPath:indexPath];
    NSString* field = [self getFieldForIndexPath:indexPath];
    
    ModifyInfoController* controller = [[ModifyInfoController alloc] initWithNibName:@"ModifyInfoController" bundle:nil];
    
    NSMutableDictionary* dict = [NSMutableDictionary dictionary];
    [dict setObject:fieldName forKey:@"fieldName"];
    [dict setObject:fieldValue forKey:@"fieldValue"];
    [dict setObject:field forKey:@"field"];
    controller.param = dict;
    
    NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(onFieldModified:) name:FIELD_UPDATE object:nil];
    
    [self.navigationController pushViewController:controller animated:YES];
    
    [controller release];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
    HistoryInfoControllerViewController* controller = [[HistoryInfoControllerViewController alloc] initWithNibName:@"HistoryInfoControllerViewController" bundle:nil];
    
    controller.field = [self getFieldForIndexPath:indexPath];
    controller.fieldName = [self getFieldNameForIndexPath:indexPath];
    
    [self.navigationController pushViewController:controller animated:YES];
    
    [controller release];
}

-(void) updateHealthInfo:(NSString*) field value:(NSString*) value
{
    [m_healthInfo setObject:value forKey:field];
    [self.tableView reloadData];
}

-(void) onFieldModified:(NSNotification*) notification
{
    NSDictionary* dict = notification.userInfo;
    NSString* field = [dict objectForKey:@"field"];
    NSString* value = [dict objectForKey:@"value"];
    
    [self updateHealthInfo:field value:value];
}

@end
